package com.example.oj.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.oj.entity.Problem;
import com.example.oj.mapper.ProblemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProblemService extends ServiceImpl<ProblemMapper, Problem> {

    @Resource
    ProblemMapper problemMapper;

    public Page<Problem> SelectPage(Integer pageNum, Integer pageSize, String search){
        LambdaQueryWrapper<Problem> wrapper = Wrappers.lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
            wrapper.like(Problem::getTitle, search).or(query->{
                query.like(Problem::getId, search);
            });
        }
        Page<Problem> Page = new Page<>(pageNum, pageSize);
        Page.addOrder(OrderItem.asc("id")); // 按照 id 排序
        return problemMapper.selectPage(Page, wrapper);
    }
}
